
function getRandom () {
  let random: any[]|Uint8Array = new Array()
  let strong = false
  let length = 1
  if (
    Uint8Array &&
    crypto &&
    crypto.getRandomValues
  ) {
    let randomArray = new Uint8Array(length)
    crypto.getRandomValues(randomArray)
    random = randomArray
    strong = true
  } else {
    let randomArray: string[] = [...Array.from(Array(length).keys()).map(item => item.toString())]
    randomArray = randomArray.map(() => ((Math.random() * (2 ** 8)) >> 0).toString())
    random = randomArray
  }
  return {
    strong, random
  }
}

export function IntRandom (option: {
  min?: number,
  max?: number
} = {}): number {
  const source = getRandom()
  const {min = null, max = null} = option
  if (
    ( min && (min % 1 !== 0 || min < 0) ) ||
    ( max && (max % 1 !== 0 || max < 0) ) ||
    ( min && max && min >= max )
  ) {
    return -1
  }
  if (min && max) {
    // 小！学！难！度！屮……
    return min + (max - min) / (2 ** 8 - 0) * source.random[0] >> 0
  } else if (min === null && max) {
    return source.random[0] * (max / (2 ** 8)) >> 0
  } else if (min && max === null) {
    return source.random[0] + min
  } else if (min === null && max === null) {
    return source.random[0]
  } else {
    return source.random[0]
  }
}
